﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml;
using Microsoft.Xna.Framework.Graphics;

namespace Fishes.Objects
{
    public class Tank
    {
        #region Properties

        public List<Fish> Fishes
        {
            get { return _fishes; }
            private set { _fishes = value; }
        }
        private List<Fish> _fishes = new List<Fish>();

        public List<Fish> FishToRemove
        {
            get { return _fishToRemove; }
            set { _fishToRemove = value; }
        }
        private List<Fish> _fishToRemove = new List<Fish>();

        public string Name
        {
            get { return _name; }
        }
        private string _name = String.Empty;

        public Vector2 Size
        {
            get { return _size; }
        }
        private Vector2 _size = new Vector2();

        public Texture2D BackgroundImage
        {
            get { return _backgroundImage; }
        }
        private Texture2D _backgroundImage = null;

        public FishManager FishManager
        {
            get { return _fishManager; }
        }
        private FishManager _fishManager = new FishManager();

        public Fish SelectedFish
        {
            get { return _selectedFish; }
        }
        private Fish _selectedFish = null;

        #endregion

        #region Public Methods

        public bool Load(string path, GraphicsDevice g)
        {
            try
            {
                FileStream fp = File.Open(path, FileMode.Open);

                XmlDocument doc = new XmlDocument();
                doc.Load(fp);

                this._name = doc.SelectSingleNode("tank/name").InnerText;

                int w = Convert.ToInt32(doc.SelectSingleNode("tank/width").InnerText);
                int h = Convert.ToInt32(doc.SelectSingleNode("tank/height").InnerText);
                this._size = new Vector2(w, h);

                if (this.BackgroundImage != null)
                {
                    this._backgroundImage.Dispose();
                    this._backgroundImage = null;
                }

                string backgroundFileName = doc.SelectSingleNode("tank/backgroundImage").InnerText;
                this._backgroundImage = Texture2D.FromFile(g, backgroundFileName, w, h);

                fp.Close();
                fp.Dispose();
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public void AddFish(int num, GraphicsDevice g)
        {
            int newFishPos = this.Fishes.Count;
            this.Fishes.AddRange(this.FishManager.GetFish(num, g));
            for (int i = newFishPos; i < newFishPos + num; i++)
            {
                Vector2 pos = new Vector2();
                pos.X = Game1.Random.Next(0, (int)this.Size.X - (int)this.Fishes[i].Size.X - 1);
                pos.Y = Game1.Random.Next(0, (int)this.Size.Y - (int)this.Fishes[i].Size.Y - 1);
                this.Fishes[i].Position = pos;
            }
        }

        public void RemoveFish(Fish fish)
        {
            this.FishToRemove.Add(fish);
        }

        public bool Destroy()
        {
            foreach (Fish fish in this.Fishes)
                fish.Destroy();

            this.Fishes.Clear();

            this.BackgroundImage.Dispose();
            this._backgroundImage = null;

            return true;
        }

        public void Update(GameTime gt)
        {
            foreach (Fish fish in this.FishToRemove)
            {
                this.Fishes.Remove(fish);
                fish.Destroy();
            }

            foreach (Fish fish in this.Fishes)
            {
                fish.Update(gt);
            }
        }

        public void Render(SpriteBatch sb)
        {
            Rectangle dest = new Rectangle();
            dest.X = 0;
            dest.Y = 0;
            dest.Width = (int)this.Size.X;
            dest.Height = (int)this.Size.Y;

            sb.Draw(this.BackgroundImage, dest, Color.White);

            foreach (Fish fish in this.Fishes)
            {
                fish.Render(sb);
            }
        }

        public void OnMouseClick(Vector2 pos)
        {
            if(this.SelectedFish == null)
            {
                foreach (Fish fish in this.Fishes)
                {
                    if (pos.X >= fish.Position.X &&
                        pos.X <= fish.Position.X + fish.Size.X &&
                        pos.Y >= fish.Position.Y &&
                        pos.Y <= fish.Position.Y + fish.Size.Y)
                    {
                        fish.IsSelected = true;
                        this._selectedFish = fish;
                        break;
                    }
                }
            }
        }

        public void OnMouseUp()
        {
            if (this.SelectedFish == null)
                return;

            this.SelectedFish.IsSelected = false;
            this._selectedFish = null;
        }

        #endregion
    }
}
